<template>
<!--
      show: 是否显示水印
      text：水印内容
-->
  <Watermark :show="false" text="小马医疗">
      <router-view></router-view>
  </Watermark>
</template>

<script setup>
import {onMounted,getCurrentInstance} from "vue";
import { useRouter} from 'vue-router'
import Watermark from '@/components/Watermark/index.vue'
import {getGenCTicket, getProjectConfig,wxSilentLogin } from "@/utils/api.js";
import {ElMessage} from "element-plus";
import {isWX} from '@/utils/tool.js'
const { proxy } = getCurrentInstance();
const { mittBus } = proxy;
const router = useRouter()
const CTK = window.localStorage.getItem('CTK') || null

const init = () =>{
  const appid = import.meta.env.APPID;
  const url = encodeURIComponent(location.origin) ;
  const access_url  = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}/home&response_type=code&scope=snsapi_base#wechat_redirect`;
  const params = new URLSearchParams(location.search)
  if (!params.get('code')) {
    location.href = access_url;
  }
}

const getWXToken = async () =>{
  const params = new URLSearchParams(location.search)
  if(params.get('code')){
    const v = await wxSilentLogin({csProjectAppid:'dhlj_wxgzh',code:params.get('code')})
    if(v?.c === '100'){
      window.localStorage.setItem('CTK',v?.d)
      mittBus.emit('getConfig')
    }
  }
}

function getToken(){
    const platform = import.meta.env.VITE_PLATFORM_IP
    getGenCTicket({platform}).then(v =>{
        if(v?.c === "100"){
            window.localStorage.setItem('CTK',v?.d)
            getConfig()
            mittBus.emit("CTK",v?.d)
        }
    })
}

const getConfig =()=>{
  getProjectConfig().then(v=>{
    if(v.c === '100'){
      localStorage.setItem('config',JSON.stringify(v.d))
      document.title = v.d?.webNavbarTitle || ''
    }
  })
}


onMounted(async () => {
  // if(isWX()){
  //   await router.push({name: 'wxerror'})
  // }
  // init()
  // await getWXToken()
  if(!CTK) {
    getToken()
  }else {
    getConfig()
  }

  if (typeof WebSocket != "undefined") {
        console.log("您的浏览器支持Websocket通信协议");
  } else {
        alert("您的浏览器不支持Websocket通信协议，请使用Chrome或者Firefox浏览器！");
  }
});

</script>

<style lang="scss">
.el-message{
  .el-badge{
    display: none;
  }
  .el-message__content{
    width: max-content;
  }
}
</style>
